//
//  YDMainTabBarController.m
//  YDTestDrive
//
//  Created by ios on 2017/2/8.
//  Copyright © 2017年 ios. All rights reserved.
//

#import "YDMainTabBarController.h"

#import "YDHomePageController.h"
#import "YDCustomerListController.h"
#import "YDMyInfoController.h"

#import "YDNavigationController.h"

#import "YDTabBar.h"

@interface YDMainTabBarController () <MainTabBarDelegate>

@property (nonatomic, strong) YDTabBar *mainTabBar;

@end

@implementation YDMainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self SetupMainTabBar];
    [self SetupAllControllers];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
}

- (void)SetupMainTabBar{
    YDTabBar *mainTabBar = [[YDTabBar alloc] init];
    mainTabBar.frame = self.tabBar.bounds;
    mainTabBar.delegate = self;
    [self.tabBar addSubview:mainTabBar];
    _mainTabBar = mainTabBar;
    
    [self.tabBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(kScreenWidth, 49.0f)]];
    [self.tabBar setShadowImage:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(kScreenWidth, 1.0f)]];
}

- (void)SetupAllControllers{
    NSArray *titles = @[@"首页", @"试驾客户", @"我的"];
    NSArray *images = @[@"tab_home_nor", @"tab_sj_nor", @"tab_me_nor"];
    NSArray *selectedImages = @[@"tab_home_sel", @"tab_sj_sel", @"tab_me_sel"];
    
    YDHomePageController *homeVc = [[YDHomePageController alloc] init];
    
    YDCustomerListController *subscriptionVc = [[YDCustomerListController alloc] init];
    
    YDMyInfoController *notificationVc = [[YDMyInfoController alloc] init];
    
    NSArray *viewControllers = @[homeVc, subscriptionVc, notificationVc];
    
    for (int i = 0; i < viewControllers.count; i++) {
        UIViewController *childVc = viewControllers[i];
        [self SetupChildVc:childVc title:titles[i] image:images[i] selectedImage:selectedImages[i]];
    }
}

- (void)SetupChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)imageName selectedImage:(NSString *)selectedImageName{
    YDNavigationController *nav = [[YDNavigationController alloc] initWithRootViewController:childVc];
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    childVc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];
    childVc.tabBarItem.title = title;
    [self.mainTabBar addTabBarButtonWithTabBarItem:childVc.tabBarItem];
    [self addChildViewController:nav];
}

#pragma mark --------------------mainTabBar delegate
- (void)tabBar:(YDTabBar *)tabBar didSelectedButtonFrom:(long)fromBtnTag to:(long)toBtnTag{
    self.selectedIndex = toBtnTag;
}

@end
